import { _decorator, Button, Component, Label, Node, ProgressBar, Sprite, tween, UIOpacity, Vec3 } from 'cc';
import { UIBase } from './UIBase';
import { GameDefine } from '../game/GameDefine';
import { EventListener } from '../core/EventListener';
import { FortressManager } from '../fortress/FortressManager';
import { UILottery } from './UILottery';
import { EnemyManger } from '../actor/EnemyManger';
import { MapManager } from '../game/MapManager';
import { AudioManager } from '../game/AudioManager';
import { GameItem } from './item/GameItem';
import { UIManager } from './UIManager';
import { Constants, Orientation } from '../game/Constants';
const { ccclass, property } = _decorator;

@ccclass('UIGame')
export class UIGame extends UIBase {
    public static Instance: UIGame = null;
    // 结束界面
    @property(Node) uiWin: Node = null!;

    @property({ type: GameItem, displayName: "横屏" })
    landscapeNode: GameItem = null;
    @property({ type: GameItem, displayName: "竖屏" })
    portraitNode: GameItem = null;


    /** Boss进度条 */
    get BossBar(): ProgressBar {
        return this.landscapeNode.node.active ? this.landscapeNode.gameBar : this.portraitNode.gameBar;
    }

    /** 金币停止位置 */
    get CoinNode(): Node {
        const targetNode = this.landscapeNode.node.active ? this.landscapeNode.goldCollectNode : this.portraitNode.goldCollectNode;
        return  targetNode;
    }

    private showSkillBtn: boolean = false;

    onLoad(): void {
        UIGame.Instance = this;
        super.onLoad();
    }

    start() {
        this.getComponent(UIOpacity).opacity = 0;
        this.initOrientationUI();
        EventListener.on(Constants.Event_OrientationChange, this.initOrientationUI, this);
        EventListener.on('game_win', () => { this.uiWin.active = true }, true);
        EventListener.on('start_boss_fight', this.playBossWarning, this);
        EventListener.on('lottery_chance', () => {
            // 显示抽奖按钮
            this.showSkillBtn = true;
            this.showSkillButton();
        }, this);
    }

    private initOrientationUI() {
        if (UIManager.Instance.orientation === Orientation.Landscape) {
            this.landscapeNode.node.active = true;
            this.portraitNode.node.active = false;
        } else {
            this.landscapeNode.node.active = false;
            this.portraitNode.node.active = true;
        }
        this.showSkillButton();
    }

    private playBossWarning() {
        // 播放音效
        AudioManager.Instance.playWarring();
        // 播放UI动画
        if (this.landscapeNode.node.active) this.landscapeNode.playBossWarning();
        if (this.portraitNode.node.active) this.portraitNode.playBossWarning();
        // 2秒后停止要塞移动并生成Boss
        this.scheduleOnce(() => {
            FortressManager.Instance.stopMove();
            EnemyManger.Instance.createBossEnemy();
        }, 2);
    }

    private showSkillButton() {
        // 横屏竖屏切换导致UI不一致问题
        if (!this.showSkillBtn) return;
        if (this.landscapeNode.node.active) this.landscapeNode.showSkillButton(true, () => { this.showSkillBtn = false; });
        if (this.portraitNode.node.active) this.portraitNode.showSkillButton(true, () => { this.showSkillBtn = false; });


    }

}


